
package net.atremmedia.gwt.actionjava.examples.bounds.samples;

import net.atremmedia.gwt.actionjava.examples.bounds.samples.curve.DrawCurve;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.geom.Point2D;
import actionjava.geom.curve.CubicCurve2D;

public class DrawCubicCurveBounds2 extends DrawCurve
{
	public static final String NAME = "Draw cubicCurve bounds 2";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bounds/src/main/java/net/atremmedia/gwt/actionjava/examples/bounds/samples/DrawCubicCurveBounds2.java";

	private Point2D p1;
	private Point2D cp1;
	private Point2D cp2;
	private Point2D p2;

	/*
	 * ---------------------------------------------------------------
	 *	Constructor & init
	 * ---------------------------------------------------------------
	 */

	public DrawCubicCurveBounds2(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	private void initialize()
	{
		// Start point
		p1 = new Point2D(getRandomNumber(25, (getWidth() - 200)), getRandomNumber(25, (getHeight() - 100)));

		// Control point 1
		cp1 = new Point2D(getRandomNumber(25, (getWidth() - 200)), getRandomNumber(25, (getHeight() - 100)));

		// Control point 2
		cp2 = new Point2D(getRandomNumber(25, (getWidth() - 200)), getRandomNumber(25, (getHeight() - 100)));

		// End point
		p2 = new Point2D(getRandomNumber(25, (getWidth() - 200)), getRandomNumber(25, (getHeight() - 100)));
	}

	/*
	 * ---------------------------------------------------------------
	 *	Abstract Overrides
	 * ---------------------------------------------------------------
	 */

	@Override
	public void run()
	{
		initialize();
		super.run(true);

		addChild(getCurveShape());
	}

	@Override
	public void drawCurve(Graphics graph)
	{
		// Cubic curve line
		graph.beginStroke("#FF0000");
		graph.moveTo(p1.x, p1.y);
		graph.bezierCurveTo(cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
		graph.endStroke();
	}

	@Override
	public void drawDots(Graphics graph)
	{
		// P1 dot
		drawDot(p1.x, p1.y, P1_DOT_COLOR, graph);

		// CP1 dot
		drawDot(cp1.x, cp1.y, CP_DOT_COLOR, graph);

		// CP2 dot
		drawDot(cp2.x, cp2.y, CP_DOT_COLOR, graph);

		// P2 dot
		drawDot(p2.x, p2.y, P2_DOT_COLOR, graph);
	}

	@Override
	public void drawControlPointLines(Graphics graph)
	{
		// Line connecting P1 / CP1 / CP2 / P2
		graph.beginStroke("#00FF00");
		graph.moveTo(p1.x, p1.y);
		graph.lineTo(cp1.x, cp1.y);
		graph.lineTo(cp2.x, cp2.y);
		graph.lineTo(p2.x, p2.y);
		graph.endStroke();
	}

	@Override
	public void setBounds()
	{
		// Cubic curve bounds (Rectangle)
		CubicCurve2D cubicCurve = new CubicCurve2D(p1, cp1, cp2, p2);

		super.setBounds(cubicCurve.getBounds());
	}
}
